package name.flurl.android.tt_news4android;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NewsDetailActivity extends Activity {

	private NewsEntry mNews;
	private TextView mDateTime;
	private TextView mTitleText;
	private WebView mBrowser;
	private Button mOpenGalleryBtn;
	private TextView mCategories;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// for setting the app icon
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		// the content has to be inflated before...
		setContentView(R.layout.news_detail);
		// ...we can set the app icon
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon);

		Bundle extras = getIntent().getExtras();
		mNews = (NewsEntry) extras.getSerializable("news");

		setTitle(mNews.getmTitle() + " - " + getString(R.string.app_name));

		mDateTime = (TextView) findViewById(R.id.newsdate);
		mCategories = (TextView) findViewById(R.id.newscategories);
		mTitleText = (TextView) findViewById(R.id.newstitle);
		mOpenGalleryBtn = (Button) findViewById(R.id.newsgallerybutton);
		mOpenGalleryBtn.setOnClickListener(openGalleryBtnListener);

		// connect to our browser so we can manipulate it
		mBrowser = (WebView) findViewById(R.id.newsdescription);
		// set a webview client to override the default functionality
		// mBrowser.setWebViewClient(new wvClient());

		mBrowser.setBackgroundColor(0);

		// get settings so we can config our WebView instance
		// WebSettings settings = mBrowser.getSettings();

		// JavaScript? Of course!
		// settings.setJavaScriptEnabled(true);

		// clear cache
		mBrowser.clearCache(true);

		// this is necessary for "alert()" to work
		// mBrowser.setWebChromeClient(new WebChromeClient());

		// add our custom functionality to the javascript environment
		// mBrowser.addJavascriptInterface(new CalculatorHandler(), "calc");

		// uncomment this if you want to use the webview as an invisible
		// calculator!
		// mBrowser.setVisibility(View.INVISIBLE);

		// load a page to get things started

		// allows the control to receive focus
		// on some versions of Android the webview doesn't handle input focus
		// properly
		// this seems to make things work with Android 2.1, but not 2.2
		// mBrowser.requestFocusFromTouch();

		// mRowId = (savedInstanceState == null) ? null : (Long)
		// savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);

		// if (mRowId == null) {
		// Bundle extras = getIntent().getExtras();
		// mRowId = (extras != null) ? extras.getLong(NotesDbAdapter.KEY_ROWID)
		// : null;
		// }

		populateFields();

	}

	private void populateFields() {

		mDateTime.setText(mNews.getmDateTimeF());

		if (!mNews.getmCategories().equals("")) {
			mCategories.setText(mNews.getmCategories());
			mCategories.setVisibility(View.VISIBLE);
		} else {
			mCategories.setVisibility(View.GONE);
		}

		LinearLayout layout = (LinearLayout) findViewById(R.id.NewsDetailFilesLinearLayout);
		final String[] newsFiles = mNews.getNewsFilesArray();

		Resources res = getResources();

		for (int i = 0; i < newsFiles.length; i++) {
			// final var needed in runnable
			final int j = i;
			Button btn = new Button(this);
			btn.setText(getString(R.string.file));
			btn.setCompoundDrawablesWithIntrinsicBounds(null, null, res
					.getDrawable(R.drawable.flyer), null);
			btn.setHeight(mOpenGalleryBtn.getHeight());
			// btn.setTextSize(16);
			btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// create an intent to open the flyer from the web
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(newsFiles[j]));
					startActivity(intent);
				}
			});
			layout.addView(btn);
		}

		if (mNews.getImagesArray() != null)
			mOpenGalleryBtn.setVisibility(View.VISIBLE);
		else
			mOpenGalleryBtn.setVisibility(View.GONE);

		mTitleText.setText(mNews.getmTitle());

		String html = "";
		html = "<h3>" + Uri.encode(mNews.getmShort()) + "</h3>" + "<p>"
				+ Uri.encode(mNews.getmBodyText()) + "</p>";

		html = "<html><head><style type=\"text/css\">* {color: #BBB;margin-left:0;margin-right:0;}</style><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" > </head><body>"
				+ html + "</body></html>";

		mBrowser.loadData(html, "text/html", "utf-8");
	}

	private OnClickListener openGalleryBtnListener = new OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(v.getContext(), GalleryActivity.class);
			i.putExtra("news", mNews);
			startActivity(i);
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.detail_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_open_in_browser:
			// Log.i("POGA Events click", "Forthcoming menu clicked");
			i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(mNews.getmNewsSingleLink()));
			startActivity(i);
			return true;
		case R.id.menu_share:
			// Log.i("POGA Events click", "Previous menu clicked");
			i = new Intent(android.content.Intent.ACTION_SEND);
			i.setType("text/plain");
			i.putExtra(Intent.EXTRA_SUBJECT,
					getString(R.string.action_send_subject));
			i.putExtra(Intent.EXTRA_TEXT, mNews.getmNewsSingleLink());
			startActivity(Intent.createChooser(i,
					getString(R.string.menu_share_title)));
			return true;
		case R.id.menu_preferences:
			// Log.i("POGA Events click", "Show latest gallery menu clicked");
			i = new Intent(this, PreferencesActivity.class);
			startActivity(i);
			return true;
		default:
			// Log.i("POGA Events click", "something went wrong");
			return super.onOptionsItemSelected(item);
		}
	}

}
